package ph.edu.upd.eee.upcatarchivesearch;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

/**
 * Created by smartwire on 10/2/2014.
 */
public class Utility {
    public static final String ARCHIVE_DIR = "/upcatfinder";
    public static final String ARCHIVE_URL = "https://www.dropbox.com/s/c9acka8ky2l8byh/upcatfinder.zip?dl=1";

    public static final String NETWORK_DISCONNECTED = "nonet";
    public static final String NETWORK_WIFI = "wifinet";
    public static final String NETWORK_ETHERNET = "ethernet";
    public static final String NETWORK_MOBILE_FAST = "mobilefastnet";
    public static final String NETWORK_MOBILE_SLOW = "mobileslownet";

    public static String bytesToSI(int bytes) {
        int exp;

        if(bytes < 1000) {
            return bytes + "B";
        }
        else {
            exp = (int) (Math.log(bytes) / Math.log(1000)); // log_1000(bytes)

            return String.format("%.1f %sB", bytes / Math.pow(1000, exp), "kMGTPE".charAt(exp - 1));
        }
    }

    public static boolean archiveCheck(Activity activity) {
        File file = new File(activity.getExternalFilesDir(null) + Utility.ARCHIVE_DIR);

        return file.exists() && file.isDirectory();
    }

    public static String getConnectionType(Context context) {
        int netSubType;

        ConnectivityManager cm;
        NetworkInfo nInfo;

        cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        nInfo = cm.getActiveNetworkInfo();

        if(nInfo == null || !nInfo.isConnected()) {
            return NETWORK_DISCONNECTED;
        }

        switch(nInfo.getType()) {
            case ConnectivityManager.TYPE_WIFI:
                return NETWORK_WIFI;
            case ConnectivityManager.TYPE_MOBILE:
                netSubType = nInfo.getSubtype();

                switch(netSubType) {
                    case TelephonyManager.NETWORK_TYPE_1xRTT:
                    case TelephonyManager.NETWORK_TYPE_CDMA:
                    case TelephonyManager.NETWORK_TYPE_EDGE:
                    case TelephonyManager.NETWORK_TYPE_IDEN:
                    case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                        return NETWORK_MOBILE_SLOW;
                    default:
                        return NETWORK_MOBILE_FAST;
                }
            case ConnectivityManager.TYPE_ETHERNET:
                return NETWORK_ETHERNET;
            default:
                return NETWORK_DISCONNECTED;
        }
    }

    public static JSONObject getUpdateStats(String path) {
        String line, strAll;
        BufferedReader bufFileReader = null;

        JSONObject jsonObject = null;

        try {
            bufFileReader = new BufferedReader(new FileReader(new File(path + "/update.json")));

            strAll = "";
            while((line = bufFileReader.readLine()) != null) {
                strAll += line;
            }

            jsonObject = new JSONObject(strAll);
        }
        catch(Exception e) {
            e.printStackTrace();
            // Do nothing...
        }
        finally {
            try {
                if (bufFileReader != null) {
                    bufFileReader.close();
                }
            }
            catch(Exception e) {
                // Do nothing...
            }
        }

        return jsonObject;
    }
}
